package com.tij.rtti.typeinfo;

import java.sql.SQLOutput;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName:
 * @author:
 * @Description: chp14 type info
 * RTTI：在运行时，识别一个对象的类型。这个例子就是将一组对象转为 List ，List中对象是Object
 * Object 转型为Shape，而不是Circle，Square，Triangle。
 * data:2024/10/6 18:24
 * @since
 **/
abstract  class Shape{

    void draw(){
        System.out.println(this + ".draw()");
    }
    abstract public String toString();
}

class Circle extends Shape{

    @Override
    public String toString() {
        return "Circle";
    }
}

class Square extends Shape{
    @Override
    public String toString() {
        return "Square";
    }
}

class Triangle extends Shape{

    @Override
    public String toString() {
        return "Triangle";
    }
}

class RhomBoid extends Shape{

    @Override
    public String toString() {
        return "RhomBoid";
    }
}
public class Shapes {

    static void rotate(Shape shape){
        if(shape instanceof Circle){
            System.out.println(" this is Circle , not do ");
        }else{
            System.out.println(" run rotate ...");
        }
    }

    public static void main(String[] args){
        List<Shape> shapeList = Arrays.asList(new Circle(),new Square(),new Triangle());
        for(Shape shape: shapeList){
            shape.draw();
            rotate(shape);
        }

        // Rhomboid 向上转型 Shape ,向下转型Rhomboid
        Shape shape = new RhomBoid();
        shape.draw();
        //先判断类型
        if(shape instanceof RhomBoid){
            System.out.println(" shape 是 RhomBoid ");
            // 向下转型 Circle
            RhomBoid rhomBoid = (RhomBoid) shape;
            rhomBoid.draw();
        }else{
            System.out.println(" no RhomBoid ");
        }


        //先判断类型
        if(shape instanceof Circle){
            System.out.println(" shape 是 Circle ");
            // 向下转型 Circle
            Circle circle = (Circle) shape;
            circle.draw();
        }else{
            System.out.println(" 非Circle ");
        }


    }
}

/** output
 Circle.draw()
 Square.draw()
 Triangle.draw()
**/